package com.java2007.hotel.service.impl;

import com.java2007.hotel.dao.FoodTypeDao;
import com.java2007.hotel.dao.impl.FoodTypeDaoImpl;
import com.java2007.hotel.pojo.FoodType;
import com.java2007.hotel.service.FoodTypeService;
import com.java2007.hotel.utils.JdbcUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * @author Reckeys
 * @version V1.0
 * @date 2021/01/22
 **/
public class FoodtypeServiceImpl implements FoodTypeService {
    private FoodTypeDao foodTypeDao = new FoodTypeDaoImpl();

    /**
     * 通过typeName查询
     * @param typeName 菜类别名
     * @return
     */
    @Override
    public List<FoodType> findByTypeName(String typeName) throws SQLException {
        if (typeName.isEmpty()){
            typeName = "";
        }
        return foodTypeDao.finfByTypeName("%" + typeName.trim() + "%");
    }

    /**
     * 通过id删除
     * @param typeId
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByTypeId(Integer typeId) throws SQLException {
        try {
            JdbcUtils.startTransactions();
            foodTypeDao.deleteById(typeId);
            JdbcUtils.commit();
            return 1;
        } catch (SQLException e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
            return 0;
        }
    }

    /**
     * 通过id查询
     * @param typeId
     * @return
     */
    @Override
    public FoodType findByTypeId(Integer typeId) throws SQLException {
        try {
            JdbcUtils.startTransactions();
            FoodType foodType = foodTypeDao.findByTypeId(typeId);
            JdbcUtils.commit();
            return foodType;
        } catch (Exception e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
            return null;
        }

    }

    /**
     * 更新信息
     * @param foodType
     * @return
     */
    @Override
    public int updateFoodType(FoodType foodType) throws SQLException {
        try {
            JdbcUtils.startTransactions();
            foodTypeDao.updateFoodType(foodType);
            JdbcUtils.commit();
            return 1;
        } catch (SQLException e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
            return 0;
        }
    }

    /**
     * 添加菜类别
     * @param foodType
     * @return
     */
    @Override
    public int addFoodType(FoodType foodType) throws SQLException {
        try {
            JdbcUtils.startTransactions();
            foodTypeDao.addFoodType(foodType);
            JdbcUtils.commit();
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
            return 0;
        }
    }
}
